﻿using System;
using Caliburn.Micro;
using Scrabble.Model;

namespace Scrabble.ViewModels
{
    public class PotentialPartOfPlay : PropertyChangedBase, ISelectable
    {
        private bool _isSelected;
        private int _positionInRack;
        private BoardLocation _location;

        public event EventHandler SelectionChanged;

        public Tile Tile { get; set; }

        public BoardLocation Location
        {
            get { return _location; }
            set
            {
                if (value == Location) return;

                _location = value;
                Tile.RestoreBlank();

                NotifyOfPropertyChange(() => Location);
                NotifyOfPropertyChange(() => IsFree);
            }
        }

        public int PositionInRack
        {
            get { return _positionInRack; }
            set
            {
                if (value == PositionInRack) return;

                _positionInRack = value;
                NotifyOfPropertyChange(() => PositionInRack);
            }
        }

        public bool IsFree
        {
            get { return Location == null; }
        }

        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value == IsSelected) return;

                _isSelected = value;
                NotifyOfPropertyChange(() => IsSelected);
                NotifyOfSelectionChanged();
            }
        }

        private void NotifyOfSelectionChanged()
        {
            var handler = SelectionChanged;
            if (handler != null) handler(this, EventArgs.Empty);
        }
    }
}